<template>
  <div>
    <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="80px" size="mini">

      <el-form-item label="机构：" prop="schoolId">
        <el-select v-model="ruleForm.schoolId"  @change="getShiTang" size="mini" clearable placeholder="请选择学校">
          <el-option
            v-for="item in schoolList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="食堂：" prop="shitangId">
        <el-select v-model="ruleForm.shitangId"   @change="getShop" size="mini" clearable placeholder="请选择食堂">
          <el-option
            v-for="item in shitangList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="档口：" prop="shopId">
        <el-select v-model="ruleForm.shopId" multiple size="mini" clearable placeholder="请选择档口">
          <el-option
            v-for="item in shopList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="星期：" prop="weekTime">
        <el-select v-model="ruleForm.weekTime" multiple placeholder="请选择星期">
          <el-option label="星期一" value=0></el-option>
          <el-option label="星期二" value=1></el-option>
          <el-option label="星期三" value=2></el-option>
          <el-option label="星期四" value=3></el-option>
          <el-option label="星期五" value=4></el-option>
          <el-option label="星期六" value=5></el-option>
          <el-option label="星期七" value=6></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="resetForm('ruleForm')">{{$t("action.reset")}}</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t("action.save")}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import Cookies from 'js-cookie'

  export default {
    name: 'upload',
    props:{
      uploadId:{
        type:Number,
        default:""
      }
    },
    data() {
      return{
        url: this.$Config.url,
        schoolList: [],//学校数据
        shitangList: [],//食堂数据
        shopList: [],//档口数据
        ruleForm: {
          schoolId:"",//机构id
          shitangId:"",//食堂id
          shopId:[],//档口id
          weekTime:[],//星期
        },
        rules: {
          schoolId: [{ required: true, message: "请选择学校", trigger: "change" }],
          shitangId: [{ required: true, message: "请选择食堂", trigger: "change" }],
          shopId: [{ required: true, message: "请选择档口", trigger: "change" }],
          weekTime:[{ required: true, message: "请选择星期", trigger: "change" }],
        },
      }
    },
    mounted() {
      this.getSchool();
    },
    methods:{
      submitForm(formName) {
        const that = this;
        this.$refs[formName].validate(valid => {
          if (valid) {
            that.send();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      send(){
        const that=this;
        const data={
          productId:this.uploadId,
          shopIds:this.ruleForm.shopId,
          weekDays:this.ruleForm.weekTime
        }

        this.$axios
          .post(
            that.url + "/admin/product/onShelf",
            data,
            {
              headers: {
                "x-baie-admin-token": Cookies.get("token")
              }
            }
          )
          .then(function(res) {
            if (res.status == 200) {
              if(res.data.code==200){
                if (that.msg == 1) {
                  that.$message({
                    message: "添加成功",
                    type: "success"
                  });
                } else {
                  that.$message({
                    message:"修改成功",
                    type: "success"
                  });
                }
                that.$emit("hideEdit", false);
              }else{
                that.$message({
                  message: res.data.msg,
                  type: "warning"
                });
              }
            } else {
              that.$message({
                message: res.statusText,
                type: "warning"
              });
            }
          })
          .catch(function(error) {
            console.log(error);
          });
      },
      getShop() {
        const that = this;
        this.$axios.get(that.url + '/admin/shop/list', {
          params: {
            marketId: that.ruleForm.shitangId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        }).then(function (res) {
          if (res.data.code == 200) {
            that.shopList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }

        }).catch(function (error) {
          console.log(error);
        });
      },
      getShiTang() {
        const that = this;
        this.$axios.get(that.url + '/admin/market/list', {
          params: {
            deptId: that.ruleForm.schoolId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        }).then(function (res) {
          if (res.data.code == 200) {
            that.shitangList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }

        }).catch(function (error) {
          console.log(error);
        });
      },
      getSchool() {
        const that = this;
        this.$axios.get(that.url + '/admin/dept/list', {
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        }).then(function (res) {
          if (res.data.code == 200) {
            that.schoolList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }

        }).catch(function (error) {
          console.log(error);
        });
      },
    }
  }
</script>

<style scoped>
  .el-select{
    width: 100%;
  }
</style>
